﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.Security;
using Entity;
using BLL;

namespace Web.admin.User
{
    public partial class AddSingle : System.Web.UI.Page
    {
        TeacherService teacherService = new TeacherService();
        AcademyService academyService = new AcademyService();
        RoleService roleService = new RoleService();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                AcademyList.DataSource = academyService.getAllAcademies();
                AcademyList.DataTextField = "ACA_NAME";
                AcademyList.DataValueField = "ACA_ID";
                AcademyList.DataBind();

                string operation = Request["OPERATION"];
                if (operation == null)
                {
                    AddTeacher();
                    return;
                }
                string teacherId = Request["id"];
                if (teacherId == null)
                    return;

                if (operation == "Modify")
                {
                    ModifyTeacher(teacherId);
                }
                else if (operation == "Delete")
                {
                    DeleteTeacher(teacherId);
                }
            }
        }

        protected void AddTeacher()
        {
            RoleList.DataSource = roleService.getAllRoles();
            RoleList.DataTextField = "ROL_NAME";
            RoleList.DataValueField = "ROL_ID";
            RoleList.DataBind();
        }

        protected void ModifyTeacher(string teacherId)
        {
            password.Visible = false;
            confirm.Visible = false;

            BoTeachers teacher = teacherService.GetTeacher(teacherId);
            IList<BoRoles> allRoles = roleService.getAllRoles();
            RoleList.DataSource = allRoles;
            RoleList.DataTextField = "ROL_NAME";
            RoleList.DataValueField = "ROL_ID";
            RoleList.DataBind();

            txtUserName.Text = teacher.TEA_USERNAME;
            txtTeacherId.Value = teacher.TEA_ID;
            //txtPwd.Text = teacher.TEA_PASSWORD;
            //txtConPwd.Text = teacher.TEA_PASSWORD;
            txtCode.Text = teacher.TEA_CODE;
            textBoxName.Text = teacher.TEA_NAME;
            foreach (ListItem item in listGender.Items)
            {
                if (teacher.TEA_GENDER == item.Value)
                    item.Selected = true;
                else
                    item.Selected = false;
            }
            txtPhone.Text = teacher.TEA_PHONE;
            txtEmail.Text = teacher.TEA_EMAIL;
            AcademyList.SelectedValue = teacher.TEA_ACADEMY;
            ManagerBox.Checked = teacher.TEA_MANAGER == 0 ? false : true;
            IList<BoRoles> roles = teacherService.GetRolesByTeacchers(teacherId);
            foreach (ListItem item in RoleList.Items)
            {
                foreach (BoRoles r in roles)
                {
                    if (item.Value == r.ROL_ID)
                    {
                        item.Selected = true;
                        break;
                    }
                }
            }
        }

        protected void DeleteTeacher(string teacherId)
        {
            
        }

        protected void btn_Save_Click(object sender, EventArgs e)
        {
            BoTeachers teacher;
            List<BoRoles> roles;
            CollectData(out teacher, out roles);
            if (txtTeacherId.Value != "")
                teacherService.ModifyTeacherAndRoles(teacher, roles);
            else
                teacherService.CreateTeacherAndRoles(teacher, roles);
            Response.Redirect("ListUser.aspx");
        }

        protected void CollectData(out BoTeachers teacher, out List<BoRoles> roles)
        {
            if (txtTeacherId.Value != "")
                teacher = teacherService.GetTeacher(txtTeacherId.Value);
            else
                teacher = new BoTeachers();
            roles = new List<BoRoles>();

            teacher.TEA_USERNAME = txtUserName.Text;
            if (txtTeacherId.Value == "")
                teacher.TEA_PASSWORD = txtPwd.Text;
            teacher.TEA_CODE = txtCode.Text;
            teacher.TEA_NAME = textBoxName.Text;
            teacher.TEA_GENDER = listGender.SelectedValue;
            teacher.TEA_PHONE = txtPhone.Text;
            teacher.TEA_EMAIL = txtEmail.Text;
            teacher.TEA_ACADEMY = AcademyList.SelectedValue;
            teacher.TEA_MANAGER = ManagerBox.Checked ? 1 : 0;
            
            foreach (ListItem item in RoleList.Items)
            {
                if (item.Selected)
                {
                    BoRoles r = new BoRoles();
                    r.ROL_ID = item.Value;
                    r.ROL_NAME = item.Text;
                    roles.Add(r);
                }
            }
        }
        
    }
}